package com.al.repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.al.entity.User;
import com.al.util.JDBCTools;

public class UserRepository {
	public List<User> findAll(){
		List<User> list = new ArrayList<>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		User temp = null;
		try {
			connection = JDBCTools.getConnection();
			//定义sql语句
			String sql = "select * from users";
			//获取执行对象
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				Integer id = resultSet.getInt(1);
				String name = resultSet.getString(2);
				Double score = resultSet.getDouble(3);
				temp = new User(id, name, score);
				list.add(temp);
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(connection, preparedStatement, resultSet);
		}
		return list;
	}
	
	public void add(Integer id, String name, Double score) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCTools.getConnection();
			String sql = "INSERT INTO users(u_id,u_name,u_score) VALUES(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			preparedStatement.setString(2, name);
			preparedStatement.setDouble(3, score);
			preparedStatement.executeUpdate();
		}  catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(connection, preparedStatement, null);
		}
	}
	
	public void deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCTools.getConnection();
			String sql = "delete from users where u_id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(connection, preparedStatement, null);
		}
	}
	 public User findById(Integer id) {
		 Connection connection = null;
		 PreparedStatement preparedStatement = null;
		 ResultSet resultSet = null;
		 User temp = null;
		 try {
			connection = JDBCTools.getConnection();
			String sql = "select * from users where u_id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				Integer id2 = resultSet.getInt(1);
				String name = resultSet.getString(2);
				Double score = resultSet.getDouble(3);
				temp = new User(id2, name, score);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCTools.release(connection, preparedStatement, resultSet);
		}
		 return temp;
	 }
	 
	 public void update(Integer id, String name, Double score) {
		 Connection connection = null;
		 PreparedStatement preparedStatement = null;
		 try {
			 connection = JDBCTools.getConnection();
			 String sql = "update users set u_name = ?, u_score = ? where u_id = ?";
			 preparedStatement = connection.prepareStatement(sql);
			 preparedStatement.setString(1, name);
			 preparedStatement.setDouble(2, score);
			 preparedStatement.setInt(3, id);
			 preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(connection, preparedStatement, null);
		} 
	 }
}
